package cn.ware.house.interfaces.service.inboundservice.impl;

import cn.ware.house.interfaces.entity.inventory.InboundOrder;
import cn.ware.house.interfaces.entity.inventory.InboundProduct;
import cn.ware.house.interfaces.mapper.inboundmapper.InboundOrderMapper;
import cn.ware.house.interfaces.mapper.inboundmapper.InboundProductMapper;
import cn.ware.house.interfaces.service.inboundservice.InboundOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class InboundOrderServiceImpl implements InboundOrderService {

    @Resource
    private InboundOrderMapper inboundOrderMapper;

    @Resource
    private InboundProductMapper inboundProductMapper;

    // 分页查询入库订单信息
    @Override
    public List<InboundOrder> getInboundOrdersByPage(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return inboundOrderMapper.selectInboundOrdersByPage(offset, pageSize);
        // 根据传入的页码（pageNum）和每页数量（pageSize）计算偏移量（offset），然后调用数据访问层的方法获取对应分页的入库订单信息
    }

    // 根据入库订单编号查询订单信息
    @Override
    public InboundOrder getInboundOrderById(Integer inboundOrderId) {
        return inboundOrderMapper.selectInboundOrderById(inboundOrderId);
        // 直接调用数据访问层的方法，根据传入的入库订单编号查询对应的订单信息
    }

    // 插入入库订单信息
    @Override
    @Transactional
    public boolean addInboundOrder(InboundOrder inboundOrder) {
        int result = inboundOrderMapper.insertInboundOrder(inboundOrder);
        //插入订单信息此时生成的入库订单编号，需要插入入库商品信息
        List<InboundProduct> inboundProducts =inboundOrder.getInboundProducts();
        //插入商品信息
        inboundProductMapper.insertInboundProduct(inboundProducts);
        return result > 0;
        // 调用数据访问层的插入方法，将入库订单信息插入数据库，根据受影响的行数判断插入是否成功，大于0表示成功
    }

    // 根据入库订单编号更新订单信息
    @Override
    public boolean updateInboundOrderById(InboundOrder inboundOrder) {
        int result = inboundOrderMapper.updateInboundOrderById(inboundOrder);
        return result > 0;
        // 调用数据访问层的更新方法，根据传入的入库订单信息更新数据库中对应的记录，根据受影响的行数判断更新是否成功
    }

    // 根据入库订单编号删除订单信息
    @Override
    public boolean deleteInboundOrderById(Integer inboundOrderId) {
        int result = inboundOrderMapper.deleteInboundOrderById(inboundOrderId);
        return result > 0;
        // 调用数据访问层的删除方法，根据传入的入库订单编号删除数据库中对应的记录，根据受影响的行数判断删除是否成功
    }
}